<template>
  <div class="container">
    <div class="table_con">
      <el-table v-loading="loading" :data="tableData" :border='true'>
        <el-table-column :formatter="formatter" prop="sort" label="顺序"></el-table-column>
        <el-table-column :formatter="formatter" prop="userName" label="广告类型">
           <template slot-scope="scope">
               {{scope.row.type == 'photo'?'图片':'商品'}}
            </template>
        </el-table-column>
        <el-table-column :formatter="formatter"  label="轮博图片">
           <template slot-scope="scope">
             <div v-if="scope.row.loopPhotos.length">
                <img style="width:120px" :src="scope.row.loopPhotos[0].fileUrl" alt="">
             </div>
            </template>
        </el-table-column>
        <el-table-column :formatter="formatter" prop="gmtCreated" label="是否展示">
            <template slot-scope="scope">
               {{scope.row.isOn == 'y'?'是':'否'}}
            </template>
        </el-table-column>
        <el-table-column prop="" label="操作">
          <template slot-scope="scope">
            <el-button @click="linkTo('bannerCheck',scope.row.id,scope.row.sort)" type="primary" size="mini">查看</el-button>
            <el-button @click="linkTo('bannerEdit',scope.row.id,scope.row.sort)" type="primary" size="mini">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pageIndex" :page-size="pageSize" :page-sizes='[10,20,30,40]' layout="total, sizes, prev, pager, next, jumper" :total="pageCount">
      </el-pagination>
    </div>
  </div>
</template>

<script>
let CONSTANT = require("../../constant/constant.js");
let common = require("../../common.js");
export default {
  data() {
    return {
      tableData: [],
      storeName:'',
      dialogVisible:false,
      pageCount: null,
      pageIndex: 1,
      pageSize: 10,
      loading: true,
      brandId: null,
      user:{
          id:'',
          brandName:''
      },
      brandArr:[]
    };
  },
  mounted() {
    this.pageIndex = this.$router.currentRoute.query.pageIndex
      ? this.$router.currentRoute.query.pageIndex
      : 1;
    this.getList();
    this.getBrandList()
  },
  components: {
    "bg-editor": () => import("../common/bg-editor.vue")
  },
  methods: {
    linkTo(path,id,sort){
        this.$router.push(path+'?id='+id+'&sort='+sort+'&pageIndex='+this.pageIndex)
    },
    getBrandList: function() {
      let url = CONSTANT.URL.LIST.findEsBrandByPage;
      let data = {
        pageIndex:1,
        pageSize: 1000
      };
      common.requestAjax(url, JSON.stringify(data), null, res => {
        let data = res.data;
        this.brandArr = data.bussData;
      });
    },
    formatter(row, column, cellValue) {
      return cellValue ? cellValue : "- -";
    },
    addBtn(){
        this.$router.push('/bannerEdit?pageIndex='+this.pageIndex)
    },  
    editBtn(obj){
        this.user = {
            id:obj.id,
            brandName:obj.brandName
        }
        this.dialogVisible = true
    },
    deleteRole:function(id) {
        this.$confirm('确定删除?', '提示', {           
            confirmButtonText: '确定',           
            cancelButtonText: '取消',           
            type: 'warning'         
        }).then(() => {           
            let  url = CONSTANT.URL.LIST.deleteEsStore;
            let param = {id:id} ;
            common.requestAjax(url,JSON.stringify(param),null,(res)=>{
                if(res.status == 200){
                    this.$message({
                        type:'success',
                        message:'删除成功!'
                    });
                    this.getList();
                }
                else{
                    this.$message({
                        type:'warning',
                        message:res.msg
                    })
                }
            })      
        }).catch(() => {           
            this.$message({             
                type: 'info',             
                message: '已取消删除'           
            });                   
        });    
    },
    handSave(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let url = CONSTANT.URL.LIST.saveEsBrand;
          common.requestAjax(url, JSON.stringify(this.user), null, res => {
            if (res.status == 200) {
              this.$message({
                type: "success",
                message: "保存成功！"
              });
              this.getList();
              this.dialogVisible = false;
              this.user = {
                id: "",
                papersName: "",
                status: "normal"
              };
               this.$router.replace({
                  name: "testAdd",
                  query: { pageIndex: this.pageIndex, id:res.data.bussData.id }
                });
            } else {
              this.$message({
                type: "warning",
                message: res.msg
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getList: function() {
      let url = CONSTANT.URL.LIST.findEsAdvertiseByPage;
      let data = {
      };
      this.loading = true;
      common.requestAjax(url, null, null, res => {
        this.loading = false;
        let data = res.data;
        data.bussData.forEach((item,index) => {
          item.sort = index+1
        });
        this.tableData = data.bussData;
        this.pageCount = data.count;
      });
    },
    search: function(e) {
      if (e.keyCode == 13) {
        this.pageIndex = 1;
        this.getList();
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getList();
    }
  }
};
</script>
